def extract_entities(text, tags):
    entities = {}
    entity = ""
    entity_type = ""

    for char, tag in zip(text, tags):
        if tag.startswith("B-"):
            # 遇到新实体，保存前一个实体
            if entity:
                entities[entity] = entity_type
            entity = char
            entity_type = tag[2:]  # 去掉 "B-"
        elif tag.startswith("I-") and entity:
            entity += char
        else:
            # 遇到 O 或不连续 I-，结束当前实体
            if entity:
                entities[entity] = entity_type
                entity = ""
                entity_type = ""

    # 结束时处理最后一个实体
    if entity:
        entities[entity] = entity_type

    return entities

# 示例数据
text = '小明的父亲患有冠心病及糖尿病，无手术外伤史及药物过敏史'
tags = ['O', 'O', 'O', 'O', 'O', 'O', 'O', 'B-DISEASE', 'I-DISEASE', 'I-DISEASE', 'O', 'B-DISEASE', 'I-DISEASE', 'I-DISEASE', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O', 'O']

# 调用函数
entities = extract_entities(text, tags)

# 输出
print(entities)
